package com.laboratory.manage.controller;

import com.laboratory.manage.entity.AssetEntity;
import com.laboratory.manage.service.AssetService;
import com.laboratory.manage.utils.PageUtils;
import com.laboratory.manage.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: qinaolong
 * @Date: 2023/05/08/9:48
 * @Description:
 */
//@CrossOrigin(origins = "*")
@RestController
@RequestMapping("/asset")
public class AssetController {

    @Autowired
    private AssetService assetService;

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    /**
     * 新增资产信息
     */
    @RequestMapping("/save")
    public R save(@RequestBody AssetEntity assetEntity){
        boolean save = assetService.save(assetEntity);
        if(save){
            return R.ok();
        }else {
            return R.error(14000, "保存失败");
        }
    }

    /**
     * 按照数组删除资产批量或者单独删除资产信息
     */
    @RequestMapping("/delete")
    public R delete (@RequestBody Long[] ids){
        if(assetService.removeByIds(Arrays.asList(ids))){
            return R.ok();
        }else {
            return R.error(14000, "删除失败");
        }
    }

    /**
     * 修改资产信息
     */
    @RequestMapping("/update")
    public R update(@RequestBody AssetEntity assetEntity){
        boolean res = assetService.updateById(assetEntity);
        if(res){
            return R.ok();
        }else{
            return R.error(14000, "更新失败");
        }
    }

    /**
     * 分页展示所有资产
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = assetService.queryPage(params);

        return R.ok().put("data", page);
    }
}
